//
//  SpecialViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class SpecialViewController: KMViewController {
    
    var urlString = ""
    fileprivate let cellId = "specialCell"
    fileprivate var dataArray = [SpecialModel]()
    
    fileprivate var tableView : UITableView!
    
    init(title:String,urlString:String) {
        super.init(nibName: nil, bundle: nil)
        
        self.title = title
        self.urlString = kmHttps + urlString
 
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        createTableView()
        getData()
    }
    
    //MARK: 创建TableView
    fileprivate func createTableView() {
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(SpecialCell.self, forCellReuseIdentifier: cellId)
        self.view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        
    }
    
    //MARK: 加载数据
    fileprivate func getData() {
        
        KMTool.requestGetData(URLString: self.urlString, parameters: nil) { (data) in
            
            let elements = ["//li[@class='item']/a[@class='image']","//li[@class='item']/a[@class='image']/img","//li[@class='item']/div/span[@class='name']","//li[@class='item']/div/span[@class='day']","//li[@class='item']/div/p","//li[@class='item']/div/a"]
            let attributes = ["href","src","text","text","text","href"]
            let keys = ["image_href","image_url","title","time","context","application_href"]
            let array = KMTool.htmlToModel(data: data, elements: elements, attributes: attributes, keys: keys)
            
            
            for i in 0..<array.count {
                
                let model = SpecialModel.setModel(with: array[i])
                self.dataArray.append(model)
            }
            
            self.tableView.reloadData()
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}


extension SpecialViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        var height = RATIO_H(maxNum: 20)

        //通过消息内容计算高度
        let textHeight = dataArray[indexPath.row].context?.size(font: RATIO(maxNum: 16), width: ScreenW-RATIO(maxNum: 20)).height
        if textHeight! > RATIO_H(maxNum: 20) + 10 {
            height += RATIO_H(maxNum: 20)
        }
        
        //通过活动时间计算高度
        if let _ = dataArray[indexPath.row].time {
            height += RATIO_H(maxNum: 5)
        }

        return RATIO_H(maxNum: 260) + height
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? SpecialCell
        cell?.model = dataArray[indexPath.row]
        cell?.button.tag = indexPath.row
        cell?.button.addTarget(self, action: #selector(applicationAction(sender:)), for: .touchUpInside)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let webVC = SpecialWebViewController.init(urlString:kmHttps +  dataArray[indexPath.row].image_href!, title: dataArray[indexPath.row].title)
        webVC.hidesBottomBarWhenPushed = true
        webVC.removeElements = ["header","app-download","footer"]
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
    //MARK: 立即申请
    @objc fileprivate func applicationAction(sender:UIButton) {
        
        let webVC = WebViewController.init(urlString: kmHttps + dataArray[sender.tag].application_href!, title: dataArray[sender.tag].title)
        webVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webVC, animated: true)
        
    }
    
}
